package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.yc.shopping.dao.GoodDao;
import com.yc.shopping.dao.GoodTypeDao;
import com.yc.shopping.util.ShoppingUtil;
import org.eclipse.swt.widgets.Table;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Dialog_BuyGoods extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Text text_gid;
	private Text text_gcode;
	private Text text_gname;
	private Text text_gnum;
	private Combo combo;
	GoodTypeDao typeDao = new GoodTypeDao();
	GoodDao dao = new GoodDao();

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Dialog_BuyGoods(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(727, 724);
		shell.setText("采购商品");
		//对话框居中
		ShoppingUtil.centerWindows(shell);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		
		//选中表格中一行
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem tableItem = (TableItem)e.item;
				System.out.println(tableItem.getText(0) + "\t" + tableItem.getText(1));
				//设置文本框
				text_gcode.setText(tableItem.getText(1));
				text_gid.setText(tableItem.getText(0));
				text_gname.setText(tableItem.getText(2));
				
			}
		});
		table.setBounds(10, 48, 670, 320);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(87);
		tblclmnNewColumn.setText("商品编号");
		
		TableColumn tableColumn_7 = new TableColumn(table, SWT.NONE);
		tableColumn_7.setWidth(113);
		tableColumn_7.setText("条形码");
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(86);
		tableColumn.setText("商品名称");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(85);
		tableColumn_1.setText("类型名称");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(61);
		tableColumn_2.setText("单位");
		
		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(64);
		tableColumn_3.setText("净重");
		
		TableColumn tableColumn_4 = new TableColumn(table, SWT.NONE);
		tableColumn_4.setWidth(78);
		tableColumn_4.setText("保质期");
		
		TableColumn tableColumn_5 = new TableColumn(table, SWT.NONE);
		tableColumn_5.setWidth(74);
		tableColumn_5.setText("进货价");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(70);
		tblclmnNewColumn_1.setText("销售价");
		
		TableColumn tableColumn_6 = new TableColumn(table, SWT.NONE);
		tableColumn_6.setWidth(58);
		tableColumn_6.setText("库存");
		
		Group group = new Group(shell, SWT.NONE);
		group.setText("采购操作");
		group.setBounds(10, 374, 670, 218);
		
		Label label_1 = new Label(group, SWT.NONE);
		label_1.setBounds(10, 35, 90, 24);
		label_1.setText("商品编号:");
		
		text_gid = new Text(group, SWT.BORDER | SWT.READ_ONLY);
		text_gid.setBounds(139, 29, 158, 30);
		
		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setBounds(10, 77, 90, 24);
		lblNewLabel.setText("条  形  码:");
		
		text_gcode = new Text(group, SWT.BORDER | SWT.READ_ONLY);
		text_gcode.setBounds(139, 77, 158, 24);
		
		Label label_2 = new Label(group, SWT.NONE);
		label_2.setBounds(355, 35, 90, 24);
		label_2.setText("商品名称:");
		
		text_gname = new Text(group, SWT.BORDER | SWT.READ_ONLY);
		text_gname.setBounds(465, 29, 175, 30);
		
		Label label_3 = new Label(group, SWT.NONE);
		label_3.setBounds(355, 77, 90, 24);
		label_3.setText("采购数量:");
		
		text_gnum = new Text(group, SWT.BORDER);
		text_gnum.setBounds(465, 77, 175, 24);
		
		Button button_buy = new Button(group, SWT.NONE);
		button_buy.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				String gid = text_gid.getText().trim();
				String num = text_gnum.getText().trim();
				
				try {
					
					int result = dao.updateBalance(Integer.parseInt(gid), Integer.parseInt(num));
					if(result <= 0) {
						ShoppingUtil.showMessage(shell, "错误提示", "采购失败");
					}
					//成功
					
					//清空数据
					text_gcode.setText("");
					text_gid.setText("");
					text_gname.setText("");
					text_gnum.setText("");
					//刷新表格
					showGoods();
					
				} catch (NumberFormatException | SQLException e1) {
					e1.printStackTrace();
				}
				
			}
		});
		button_buy.setBounds(506, 154, 114, 34);
		button_buy.setText("采购");
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(10, 18, 90, 24);
		label.setText("商品类型:");
		
		combo = new Combo(shell, SWT.NONE);
		combo.setBounds(126, 10, 194, 24);
		
		Button btn_find = new Button(shell, SWT.NONE);
		btn_find.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showGoods();
				
				
			}
		});
		btn_find.setBounds(423, 8, 114, 34);
		btn_find.setText("查询");
		
		//当所有组件加载完成后，加载数据
		showtypes();
		showGoods();

	}
	public void showGoods() {
		//从下拉列表中获取数据
		String tname = combo.getText().trim();
		List<Map<String,Object>> list = null;
		try {
			if("全部".equals(tname)){
				list = dao.findAll();
			}else{
				list = dao.findByTname(tname);
			}
			if(list == null || list.isEmpty()) {
				ShoppingUtil.showMessage(shell, "消息提示", "暂无商品数据");
				return;
			}
			//清空表格中原有的数据
			table.removeAll();
			TableItem tableItem = null;
			for(Map<String,Object> m : list) {
				tableItem = new TableItem(table,SWT.NONE);
				tableItem.setText(new String[] {
						ShoppingUtil.objToString(m.get("GID")),
						ShoppingUtil.objToString(m.get("GCODE")),
						ShoppingUtil.objToString(m.get("GNAME")),
						ShoppingUtil.objToString(m.get("TNAME")),
						ShoppingUtil.objToString(m.get("UNIT")),
						ShoppingUtil.objToString(m.get("WEIGHT")),
						ShoppingUtil.objToString(m.get("GDATE")),
						ShoppingUtil.objToString(m.get("PRICE")),
						ShoppingUtil.objToString(m.get("SALEPRICE")),
						ShoppingUtil.objToString(m.get("BALANCE"))
				});
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void showtypes() {
		try {
			List<Map<String,Object>> list = typeDao.findAll();
		
			String[] infos = new String[list.size() + 1];
			infos[0] = "全部";
			for(int i = 0;i<list.size();i++) {
				Map<String,Object> m =list.get(i);
				//下拉列表 1-肉类
				infos[i + 1] = ShoppingUtil.objToString(ShoppingUtil.objToString(m.get("TNAME")));
				
			}
			//设置到下拉列表中
			combo.removeAll();//清除下拉列表数据
			combo.setItems(infos);	
			combo.select(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	}
}
